﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Marmat.Template.Report
{
    /// <summary>
    /// 报表支持对象
    /// </summary>
    public class Report
    {
        /// <summary>
        /// 报名名称
        /// </summary>
        public string Name { get; private set; }

        /// <summary>
        /// 报表标题
        /// </summary>
        public string Text { get; set; }

        /// <summary>
        /// 获取打印设置的信息。
        /// </summary>
        public PrintSettings PrintSettings { get; } = new PrintSettings();

        /// <summary>
        /// 加载报表
        /// </summary>
        /// <param name="name"></param>
        public void Load(string name)
        {
            this.Name = name;
            this.BaseReport = new FastReport.Report();
            this.BaseReport.SetName(this.Name);
            this.BaseReport.FileName = this.Name + ".frx";
            string template = this.LoadTemplate(name);
            this.LoadFromString(template);
        }

        /// <summary>
        /// 根据名称加载报表模板
        /// </summary>
        /// <param name="name">报表名称</param>
        /// <returns></returns>
        protected virtual string LoadTemplate(string name)
        {
            string result = string.Empty;
            string fileName = System.IO.Path.Combine(Application.StartupPath, $"{name}.frx");
            if (System.IO.File.Exists(fileName))
                result = System.IO.File.ReadAllText(fileName);
            return result;
        }


        /// <summary>
        /// 保存报表模板至指定位置
        /// </summary>
        /// <param name="template">模板内容</param>
        public virtual void Save(string template)
        {
            string fileName = System.IO.Path.Combine(Application.StartupPath, $"{this.Name}.frx");
            System.IO.File.WriteAllText(fileName, template);
        }

        /// <summary>
        /// 从字符串加载报表模板
        /// </summary>
        /// <param name="template"></param>
        private void LoadFromString(string template)
        {
            this.BaseReport.LoadFromString(template);
        }


        static bool initialization = false;
        private void Initialization()
        {
            lock (this)
            {
                if (initialization == false)
                {
                    initialization = true;
                    FastReport.Utils.Config.PreviewSettings.Icon = null;
                    using (MemoryStream ms = new MemoryStream(Marmat.Template.Report.Properties.Resources.Chinese__Simplified_))
                    {
                        FastReport.Utils.Res.LoadLocale(ms);
                    }
                }
            }
        }

        /// <summary>
        /// 底层报表实例
        /// </summary>
        protected FastReport.Report BaseReport { get; set; }

        /// <summary>
        /// 底层报表实例
        /// </summary>
        internal FastReport.Report BaseReportObject { get => this.BaseReport; }

        /// <summary>
        /// 注册数据
        /// </summary>
        /// <param name="data">数据</param>
        public virtual void RegisterData(System.Data.DataSet data)
        {
            this.BaseReport.RegisterData(data);
        }

        /// <summary>
        /// 注册数据
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="name">名称</param>
        public virtual void RegisterData(DataSet data, string name)
        {
            this.BaseReport.RegisterData(data, name);
        }

        /// <summary>
        /// 设置参数值
        /// </summary>
        /// <param name="complexName">参数名称</param>
        /// <param name="value">参数值</param>
        public void SetParameterValue(string complexName, object value)
        {
            this.BaseReport.SetParameterValue(complexName, value);
        }

        /// <summary>
        /// 获取参数值。
        /// </summary>
        /// <param name="complexName">参数名称</param>
        /// <returns></returns>
        public object GetParameterValue(string complexName)
        {
            return this.BaseReport.GetParameterValue(complexName);
        }

        /// <summary>
        /// 查找对象
        /// </summary>
        /// <param name="name">对象名称</param>
        /// <returns></returns>
        public FastReport.Base FindObject(string name)
        {
            return this.BaseReport.FindObject(name);
        }

        /// <summary>
        /// 注册数据
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="name">名称</param>
        public virtual void RegisterData(DataTable data, string name)
        {
            this.BaseReport.RegisterData(data, name);
        }

        /// <summary>
        /// 注册数据
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="name">名称</param>
        /// <param name="maxNestingLevel">最大层级</param>
        public virtual void RegisterData(IEnumerable data, string name, int maxNestingLevel)
        {
            this.BaseReport.RegisterData(data, name, maxNestingLevel);
        }

        /// <summary>
        /// 注册数据
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="name">名称</param>
        public virtual void RegisterData(IEnumerable data, string name)
        {
            this.BaseReport.RegisterData(data, name);
        }

        /// <summary>
        /// 保存为字符串
        /// </summary>
        /// <returns></returns>
        public string SaveToString()
        {
            return this.BaseReport.SaveToString();
        }

        /// <summary>
        /// 保存为base64的字符串
        /// </summary>
        /// <returns></returns>
        public string SaveToStringBase64()
        {
            return this.BaseReport.SaveToStringBase64();
        }

        /// <summary>
        /// 打开设计界面
        /// </summary>
        public virtual void Design()
        {
            this.Initialization();
            ReportDesignForm frm = new ReportDesignForm(this);
            this.Show(frm);
        }


        /// <summary>
        /// 显示窗体时触发
        /// </summary>
        /// <param name="baseForm"></param>
        protected virtual void Show(System.Windows.Forms.Form baseForm)
        {
            baseForm.ShowDialog();
        }

        /// <summary>
        /// 打开预览界面
        /// </summary>
        public virtual void Prepare()
        {
            this.Initialization();
            ReportPrepareForm frm = new ReportPrepareForm();
            this.BaseReport.Prepare();
            frm.previewControl1.AddTab(this.BaseReport, this.Name);
            this.Show(frm);
        }

        /// <summary>
        /// 打印报表
        /// </summary>
        public virtual void Print()
        {
            if (!string.IsNullOrEmpty(this.PrintSettings.Printer))
                this.BaseReport.PrintSettings.Printer = this.PrintSettings.Printer;
            this.BaseReport.PrintSettings.ShowDialog = this.PrintSettings.ShowDialog;
            this.BaseReport.Print();
        }
    }
}
